package com.sky.city;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.ButterKnife;


public class PullToRefreshHelper<T> implements BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener {

    private Context context;
    private int page = 1;
    private int pageSize = 10;
    private BaseQuickAdapter baseQuickAdapter;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private SmartRefreshLayout smartRefreshLayout;
    private OnLoadMoreRequestedListener onLoadMoreRequestedListener;
    private OnRefreshRequestedListener onRefreshRequestedListener;
    private int enptyLayoutResId;
    private View emptyView;

    public PullToRefreshHelper(Context context) {
        this.context = context;
    }

    public static PullToRefreshHelper Inject(Context context) {
        PullToRefreshHelper instance = new PullToRefreshHelper(context);
        return instance;
    }


    public PullToRefreshHelper setPage(int page) {
        this.page = page;
        return this;
    }

    public int getPage() {
        return page;
    }

    public PullToRefreshHelper setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public PullToRefreshHelper setBaseQuickAdapter(BaseQuickAdapter baseQuickAdapter) {
        this.baseQuickAdapter = baseQuickAdapter;
        return this;
    }

    public PullToRefreshHelper setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        return this;
    }

    public PullToRefreshHelper setLinearLayoutManager(LinearLayoutManager linearLayoutManager) {
        this.linearLayoutManager = linearLayoutManager;
        return this;
    }

    public PullToRefreshHelper setSmartRefreshLayout(SmartRefreshLayout smartRefreshLayout) {
        this.smartRefreshLayout = smartRefreshLayout;
        return this;
    }

    public PullToRefreshHelper setOnLoadMoreRequestedListener(OnLoadMoreRequestedListener onLoadMoreRequestedListener) {
        this.onLoadMoreRequestedListener = onLoadMoreRequestedListener;
        return this;
    }

    public PullToRefreshHelper setOnRefreshRequestedListener(OnRefreshRequestedListener onRefreshRequestedListener) {
        this.onRefreshRequestedListener = onRefreshRequestedListener;
        return this;
    }

    public PullToRefreshHelper setEnptyLayoutResId(int enptyLayoutResId) {
        this.enptyLayoutResId = enptyLayoutResId;
//        ButterKnife.bind(enptyLayoutResId);
        return this;
    }

    public PullToRefreshHelper setEmptyView(View emptyView) {
        this.emptyView = emptyView;
        ButterKnife.bind(emptyView);
        return this;
    }

    public PullToRefreshHelper build() {
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(baseQuickAdapter);
        baseQuickAdapter.bindToRecyclerView(recyclerView);
        if(smartRefreshLayout!=null){
            smartRefreshLayout.setOnRefreshListener(this);
        }
//        baseQuickAdapter.setOnLoadMoreListener(this, recyclerView);

        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            if (onLoadMoreRequestedListener != null) {
                onLoadMoreRequestedListener.onLoadMore();
            }
        });
        return this;
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMoreRequested() {
//        page++;
//        if (onLoadMoreRequestedListener != null) {
//            onLoadMoreRequestedListener.onLoadMore();
//        }
    }

    /**
     * 刷新
     *
     * @param refreshLayout
     */
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        //todo 设置属性 为了解决上拉加载和下拉刷新造成的冲突
        if (onRefreshRequestedListener != null) {
            onRefreshRequestedListener.onRefresh(refreshLayout);
            baseQuickAdapter.setEnableLoadMore(false);
            if(smartRefreshLayout!=null){
                smartRefreshLayout.setEnableRefresh(true);
            }
        }
    }

    /**
     * 设置加载和刷新的数据
     *
     * @param
     * @param data
     */

    public void setData( List<T> data) {
        //可以在此加上各种视图封装
        if(smartRefreshLayout!=null){
            smartRefreshLayout.finishRefresh();
        }
        boolean isRefresh=page==1?true:false;

        final int size = data == null ? 0 : data.size();
        smartRefreshLayout.finishRefresh();
        if (isRefresh) {
            baseQuickAdapter.setNewData(data);
            if(data.size()<=0){
                if(enptyLayoutResId>0)baseQuickAdapter.setEmptyView(enptyLayoutResId);
                if (emptyView!=null) baseQuickAdapter.setEmptyView(emptyView);
            }
        } else {
            if (size > 0) {
                baseQuickAdapter.addData(data);
                smartRefreshLayout.finishLoadMore();
            } else {
                smartRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    }

    public interface OnLoadMoreRequestedListener {
        public void onLoadMore();
    }

    public interface OnRefreshRequestedListener {
        public void onRefresh(RefreshLayout refreshLayout);
    }
}
